﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using DTO;

namespace DAO
{
    
    public class KhachHang_DAO
    {
        dbConnection db;

        public DataTable LayDanhSachKhachHang()
        {
            db = new dbConnection();
            return db.executeQuery("SELECT * FROM KHACHHANG");
        }
        //public DataTable LayDanhSachKhachHang()
        //{
        //    db = new dbConnection();
        //    return db.executeQuery("SELECT * FROM KHACHHANG");
        //}
        public DataTable TimKiemTheoMaKhachHang(string ma)
        {
            db = new dbConnection();
            string sqlString = "select * from KHACHHANG WHERE MaKH=@ma";
            SqlParameter param = new SqlParameter("@ma", ma);
            return db.executeQuery(sqlString,param);
        }

        public DataTable TimKiemTheoTenKhachHang(string ten)
        {
            db = new dbConnection();
            string sqlString = "select * from khachhang where rtrim(tenkh)=@ten";
            SqlParameter sqlParam = new SqlParameter("@ten", ten);
            return db.executeQuery(sqlString, sqlParam);
        }

        public bool ThemKhachHang(KhachHang_DTO kh)
        {
            db = new dbConnection();
            string cmdstring = "insert into KHACHHANG(MaKH,HoKH,Tenlot,TenKH,Diachi,Phai,Sdt) values (@Ma,@Ho,@Tenlot,@TenKH,@Diachi,@Phai,@Sdt);";
            SqlParameter[] param = new SqlParameter[7];
            param[0] = new SqlParameter("@Ma", kh.MaKH);
            param[1] = new SqlParameter("@Ho", kh.HoKH);
            param[2] = new SqlParameter("@Tenlot", kh.TenLot);
            param[3] = new SqlParameter("@TenKH", kh.TenKH);
            param[4] = new SqlParameter("@Diachi", kh.DiaChi);
            param[5] = new SqlParameter("@Phai", kh.Phai);
            param[6] = new SqlParameter("@Sdt", kh.SDT);
            return db.executeNonQuery(cmdstring, param);
        }

        public bool XoaKhachHang(KhachHang_DTO kh)
        {
            db = new dbConnection();
            string cmdstring = "delete from KHACHHANG where MaKH=@ID";
            SqlParameter param = new SqlParameter("@ID", kh.MaKH);
            return db.executeNonQuery(cmdstring, param);
        }

        public bool SuaKhachHang(KhachHang_DTO kh)
        {
            db = new dbConnection();
            string cmdstring = "update KHACHHANG set HoKH=@Ho,Tenlot=@Tenlot,TenKH=@TenKH,Diachi=@Diachi,Phai=@Phai,Sdt=@sdt where MaKH=@MaKH";
            SqlParameter[] param = new SqlParameter[7];
            param[0] = new SqlParameter("@MaKH", kh.MaKH);
            param[1] = new SqlParameter("@Ho", kh.HoKH);
            param[2] = new SqlParameter("@Tenlot", kh.TenLot);
            param[3] = new SqlParameter("@TenKH", kh.TenKH);
            param[4] = new SqlParameter("@Diachi", kh.DiaChi);
            param[5] = new SqlParameter("@Phai", kh.Phai);
            param[6] = new SqlParameter("@Sdt", kh.SDT);
            return db.executeNonQuery(cmdstring, param);
        }

    }
}
